home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue32 / forms / PrntDemo / viewform.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1998-02-21  |  3.1 KB  |  126 lines

  1. unit Viewform;
  2. {$H-}
  3.  
  4. interface
  5.  
  6. uses
  7.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  8.   Forms, Dialogs, StdCtrls, Buttons, ExtCtrls, Printers;
  9.  
  10. type
  11.   TCCForm = class(TForm)
  12.     Memo1: TMemo;
  13.     Panel1: TPanel;
  14.     BitBtn1: TBitBtn;
  15.     BitBtn2: TBitBtn;
  16.     Edit1: TEdit;
  17.     Label1: TLabel;
  18.     Edit2: TEdit;
  19.     Label2: TLabel;
  20.     BitBtn3: TBitBtn;
  21.     ComboBox1: TComboBox;
  22.     Label3: TLabel;
  23.     ComboBox2: TComboBox;
  24.     Label4: TLabel;
  25.     procedure BitBtn1Click(Sender: TObject);
  26.     procedure BitBtn2Click(Sender: TObject);
  27.     procedure FormShow(Sender: TObject);
  28.     procedure ComboBox1Change(Sender: TObject);
  29.   private
  30.     { Private declarations }
  31.     procedure SetFileToShow(Value: string);
  32.   public
  33.     { Public declarations }
  34.     property FileToShow: string write SetFileToShow;
  35.   end;
  36.  
  37. var
  38.   CCForm: TCCForm;
  39.  
  40. implementation
  41.  
  42. {$R *.DFM}
  43.  
  44. procedure TCCForm.SetFileToShow(Value: string);
  45. begin
  46.   Memo1.Lines.Clear;
  47.   Memo1.Lines.LoadFromFile(Value);
  48. end;
  49.  
  50. procedure TCCForm.BitBtn1Click(Sender: TObject);
  51. begin
  52.   Close;
  53. end;
  54.  
  55. procedure TCCForm.BitBtn2Click(Sender: TObject);
  56. var
  57.   F: TextFile;
  58.   i: integer;
  59. begin
  60.   { print memo.lines }
  61.   Printer.Title := 'Form Print Demo';
  62.   Printer.Canvas.Font.Name := ComboBox2.Text;
  63.   Printer.Canvas.Font.Size := 10;
  64.   AssignPrn(F);
  65.   Rewrite(F);
  66.   try
  67.     for i := 0 to Memo1.Lines.Count-1 do
  68.       Writeln(F, Memo1.Lines[i]);
  69.   finally
  70.     CloseFile(F);
  71.   end;
  72. end;
  73.  
  74. procedure TCCForm.FormShow(Sender: TObject);
  75. var
  76.   i: LongInt;
  77.   j: integer;
  78.   F: TFileStream;
  79.   Index: file of LongInt;
  80.   S: string;
  81. begin
  82.   { set screen elements }
  83.   ComboBox1.Items.Assign(Screen.Fonts);
  84.   ComboBox1.ItemIndex := ComboBox1.Items.IndexOf(Memo1.Font.Name);
  85.   ComboBox2.Items.Assign(Printer.Fonts);
  86.   ComboBox2.ItemIndex := ComboBox2.Items.IndexOf(Memo1.Font.Name);
  87.   { assign and open index file }
  88.   AssignFile(Index, ExtractFilePath(Application.ExeName)+'FILLSUM.NDX');
  89.   Reset(Index);
  90.   { open form file }
  91.   F := TFileStream.Create(ExtractFilePath(Application.ExeName)+'FILLSUM.FRM', fmOpenWrite);
  92.   try
  93.     { iterate through index offsets }
  94.     for j := 0 to FileSize(Index)-1 do
  95.     begin
  96.       Read(Index, i);
  97.       { seek to offset }
  98.       F.Seek(i, 0);
  99.       case j of { case selector controls data output }
  100.         { note: fields are formatted to fit designed
  101.           field size - see article for alternate method }
  102.         0: S := Format('%-30s', [Edit1.Text]);
  103.         1: S := Format('%14s', [Edit2.Text]);
  104.         2: S := Format('%-10s', [DateToStr(Date)]);
  105.         3: S := Format('%-8s', ['N/A']);
  106.        else S := Format('%10s', ['N/A']);
  107.       end;
  108.       { write data to stream }
  109.       F.Write(S[1], Length(S));
  110.     end;
  111.   finally
  112.     { clean up }
  113.     F.Free;
  114.     CloseFile(Index);
  115.   end;
  116.   { show file in memo }
  117.   CCForm.FileToShow := ExtractFilePath(Application.ExeName)+'FILLSUM.FRM';
  118. end;
  119.  
  120. procedure TCCForm.ComboBox1Change(Sender: TObject);
  121. begin
  122.   Memo1.Font.Name := ComboBox1.Text;
  123.   ComboBox2.ItemIndex := ComboBox2.Items.IndexOf(Memo1.Font.Name);
  124. end;
  125.  
  126. end.
  127.